package com.jiyi.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.Spinner;
import android.widget.TextView;

import com.google.gson.Gson;
import com.jiyi.adapter.CommonAdapter;
import com.jiyi.adapter.ViewHolder;
import com.jiyi.bean.supervision.SupervisionOwnerDetailJson.SuperOwnerDetial;
import com.jiyi.bean.supervision.SupervisionOwnerDetailJson.SupervisionMoveBean;
import com.jiyi.inteface.OwnerMovePopInterface;
import com.swift.maintenance.R;

public class OwnerMovePopView {

	private Context context;
	private PopupWindow mPopupWindow;
	private String selectId = "0";
	TextView tvOwnerName;
	TextView tvWbName;
	TextView tvMover;
	TextView tvMove;
	Spinner reciver;
	OwnerMovePopInterface mOwnerMovePopInterface;
	
	public OwnerMovePopView(Context context,OwnerMovePopInterface ownerMovePopInterface) {
		this.context = context;
		this.mOwnerMovePopInterface = ownerMovePopInterface;
		init();
	}

	/**
	 * @author: HYM
	 * @Title: init
	 * @Description: TODO(初始化一个新的PopWindow)
	 * @param:
	 * @return: void
	 * @return: 2017-5-5
	 * @throws
	 */
	private void init() {
		// 得到内容布局
		View view = LayoutInflater.from(context).inflate(
				R.layout.pop_supervision_move, null);

		tvOwnerName = (TextView) view.findViewById(R.id.tvOwnerName);
		tvWbName = (TextView) view.findViewById(R.id.tvWbName);
		tvMover = (TextView) view.findViewById(R.id.tvMover);
		reciver = (Spinner) view.findViewById(R.id.reciver);

		// 确定按钮
		tvMove = (TextView) view.findViewById(R.id.tvMove);

		mPopupWindow = new PopupWindow(view,
				LinearLayout.LayoutParams.MATCH_PARENT,
				LinearLayout.LayoutParams.WRAP_CONTENT);
		mPopupWindow.setFocusable(true); // 让popWindow获取焦点
		mPopupWindow.setOutsideTouchable(true);
		mPopupWindow.setBackgroundDrawable(new BitmapDrawable()); // 设置popWindow背景颜色
		mPopupWindow.setAnimationStyle(R.style.mypopwindow_anim_style);
		mPopupWindow.setOnDismissListener(new OnDismissListener() {

			@Override
			public void onDismiss() {
				hidePopWindow();
			}
		});
	}

	/**
	 * @author: HYM
	 * @Title: showPopWindow
	 * @Description: TODO(显示弹出菜单)
	 * @param:
	 * @return: void
	 * @return: 2017-4-26
	 * @throws
	 */
	public void showPopWindow(SuperOwnerDetial superOwnerDetial,final String ownerId,final int position) {

		if (superOwnerDetial != null) {
			tvOwnerName.setText(superOwnerDetial.getCompany_name());
			tvWbName.setText(superOwnerDetial.getBranch_name());
			tvMover.setText(superOwnerDetial.getSupervise_name());
			tvMove.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					mOwnerMovePopInterface.OnSureListener(new Gson().fromJson(reciver.getSelectedItem().toString(),SupervisionMoveBean.class).getReceiver_id(),ownerId,position);
				}
			});

			CommonAdapter<SupervisionMoveBean> stateUnitAdapter;
			stateUnitAdapter = new CommonAdapter<SupervisionMoveBean>(context, superOwnerDetial.getSuperLists(), R.layout.item_spinner) {

				@Override
				public void convert(ViewHolder holder, SupervisionMoveBean t) {
					holder.setText(R.id.state_name, t.getReceiver_name());
				}
			};
			
//			ArrayAdapter<String> stateAdapter;
//			String[] aaa = new String[superOwnerDetial.getSuperLists().size()];
//			for (int i = 0; i < superOwnerDetial.getSuperLists().size(); i++) {
//				aaa[i] = superOwnerDetial.getSuperLists().get(i)
//						.getName();
//			}
			// 设置报修状态的适配器
//			stateAdapter = new ArrayAdapter<String>(context,
//					R.layout.item_equipment_repair_state, aaa);

			reciver.setAdapter(stateUnitAdapter);
		}

		mPopupWindow
				.showAtLocation(new TextView(context), Gravity.CENTER, 0, 0);
		backgroundAlpha(0.5F);
	}

	/**
	 * @author: HYM
	 * @Title: hidePopWindow
	 * @Description: TODO(关闭弹出菜单)
	 * @param:
	 * @return: void
	 * @return: 2017-4-26
	 * @throws
	 */
	public void hidePopWindow() {
		if (mPopupWindow.isShowing()) {
			mPopupWindow.dismiss();
		}
		backgroundAlpha(1F);
	}

	/**
	 * 设置添加屏幕的背景透明度
	 * 
	 * @param bgAlpha
	 */
	public void backgroundAlpha(float bgAlpha) {
		Activity activity = (Activity) context;
		WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
		lp.alpha = bgAlpha; // 0.0-1.0
		activity.getWindow().setAttributes(lp);
		activity.getWindow().addFlags(
				WindowManager.LayoutParams.FLAG_DIM_BEHIND);
	}
}
